package com.travel.server.manager.impl.command.customer.data;

import com.travel.common.CommonConstant;
import com.travel.common.util.CollectionUtil;
import com.travel.common.util.CommonUtil;
import com.travel.common.util.DateUtil;
import com.travel.server.manager.dto.request.customer.data.QueryTrendIndexRequest;
import com.travel.server.manager.dto.response.customer.data.QueryTrendIndexResponse;
import com.travel.server.manager.dto.response.customer.data.QueryTrendIndexResponse.TrendIndexItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.CustomerDal;
import com.travel.server.manager.model.dto.data.CustomerDataDto;
import com.travel.server.manager.model.dto.data.DealCustomerAnalysisDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/12/10 16:22
 * @Desc:
 */
@Component
@Slf4j
public class QueryTrendIndexCommand extends AbstractCommand<QueryTrendIndexRequest, QueryTrendIndexResponse> {

    private final static Integer TYPE_CUSTOMER = 1;

    private final static Integer TYPE_PAY = 2;

    private final static Integer TYPE_VISIT = 3;

    @Autowired
    private CustomerDal customerDal;

    @Override
    protected void checkInput(QueryTrendIndexRequest request) {
        checkTimeInput(request);
        if(request.getTrendType() == null || request.getTrendType() < 1 || request.getTrendType() > 3){
            throwInvalidParam();
        }
    }

    @Override
    public QueryTrendIndexResponse handle(QueryTrendIndexRequest request) {
        checkInput(request);

        Integer type = request.getTrendType();
        Date startTime = DateUtil.getDataTime(request.getStartTime());
        Date endTime = DateUtil.getDataEndTime(request.getEndTime());
        List<TrendIndexItem> list;
        if(TYPE_CUSTOMER.equals(type)){
            List<CustomerDataDto> customerDataList = customerDal.selectCustomerAnalysis(null, endTime);
            list = makeCustomer(customerDataList, startTime, endTime);
        }else if(TYPE_PAY.equals(type)){
            List<DealCustomerAnalysisDto> customerOrderDtoList = customerDal.selectDealCustomerOrder(null, startTime, endTime, CommonConstant.YES);
            list = makePay(customerOrderDtoList, startTime, endTime);
        }else{
            list = makeVisit(startTime, endTime);
        }

        QueryTrendIndexResponse response = new QueryTrendIndexResponse();
        response.setTrendList(list);

        return response;
    }

    private List<TrendIndexItem> makeVisit(Date startTime, Date endTime){
        List<TrendIndexItem> trendList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);
        while(calendar.getTime().before(endTime) || calendar.getTime().equals(endTime)) {
            TrendIndexItem item = new TrendIndexItem();
            item.setDate(calendar.getTime());
            item.setOldCustomer("0");
            item.setNewCustomer("0");
            trendList.add(item);
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }
        return trendList;
    }

    private List<TrendIndexItem> makePay(List<DealCustomerAnalysisDto> list, Date startTime, Date endTime){
        List<TrendIndexItem> trendList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);
        while(calendar.getTime().before(endTime) || calendar.getTime().equals(endTime)) {
            TrendIndexItem item = new TrendIndexItem();
            item.setDate(calendar.getTime());
            int newCustomer = 0;
            int oldCustomer = 0;
            if(!CollectionUtil.isEmpty(list)) {
                for (DealCustomerAnalysisDto dto : list) {
                    Date date = DateUtil.getDataTime(dto.getDays());
                    if (date.before(calendar.getTime())) {
                        oldCustomer += dto.getTotalPay();
                    }
                    if(date.equals(calendar.getTime())){
                        newCustomer += dto.getTotalPay();
                    }
                }
            }
            item.setNewCustomer(CommonUtil.fenToYuan(newCustomer).toString());
            item.setOldCustomer(CommonUtil.fenToYuan(oldCustomer).toString());
            trendList.add(item);
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }
        return trendList;
    }

    private List<TrendIndexItem> makeCustomer(List<CustomerDataDto> list, Date startTime, Date endTime){
        List<TrendIndexItem> trendList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);
        while(calendar.getTime().before(endTime) || calendar.getTime().equals(endTime)){
            TrendIndexItem item = new TrendIndexItem();
            item.setDate(calendar.getTime());
            int newCustomer = 0;
            int oldCustomer = 0;
            if(!CollectionUtil.isEmpty(list)) {
                for (CustomerDataDto dto : list) {
                    Date date = DateUtil.getDataTime(dto.getDays());
                    if (date.before(calendar.getTime())) {
                        oldCustomer += dto.getTotal();
                    }
                    if(date.equals(calendar.getTime())){
                        newCustomer += dto.getTotal();
                    }
                }
            }
            item.setNewCustomer(newCustomer + "");
            item.setOldCustomer(oldCustomer + "");
            trendList.add(item);
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }
        return trendList;
    }
}
